package app;

import java.util.*;
import java.text.*;

import org.w3c.dom.Element;

import com.StarMicronics.StarIOSDK.R;

import utils.FilePack;
import utils.IntConvert;
import utils.XMLReader;

import android.content.Context;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainObject implements NotificationListener, RequestListener {
	public TicketList ticketsDataSource;
	private RelativeLayout view;
	private ListView list;
	private MyAdapter adapter;
	private RelativeLayout bottom;
	private Button btn_go;
	public String term;
	private Button backButton;
	private TicketView ticketView;
	private TimeButton btn_time;
	
	private WaittingAlertView waittingAlertView;
	private TextView[] times = new TextView[6];
	private FrameLayout[] times_frame = new FrameLayout[6];
	private String[] str_time_keys = new String[6];
	private String[] c_time_keys = new String[6];

	public MainObject() {
		ticketsDataSource = new TicketList();
		timer.schedule(task, 30000, 30000);
		ticketsDataSource.refresh();
		ticketView = new TicketView();
		
		loadMainView();

		AppDelegate.sharedInstance().layout.addView(view);
		ticket_view_layout.addView(ticketView.view);


		btn_time = (TimeButton) ticketView.view.findViewById(R.id.timeButton);
		btn_time.setOnClickListener(new OnClickListener(){
        	public void onClick(View v) {
        		time_btn_clicked();
        	}
		});
        times[0] = ticketView.time_left_plant;
        times_frame[0] = ticketView.time_left_plant_layout;
        str_time_keys[0] = TicketList.STR_TIME_LEFT_PLANT;
        c_time_keys[0] = TicketList.C_TIME_LEFT_PLANT;
        times[1] = ticketView.time_arrived_job;
        times_frame[1] = ticketView.time_arrived_job_layout;
        str_time_keys[1] = TicketList.STR_TIME_ARRIVED_JOB;
        c_time_keys[1] = TicketList.C_TIME_ARRIVED_JOB;
        times[2] = ticketView.time_start_pour;
        times_frame[2] = ticketView.time_start_pour_layout;
        str_time_keys[2] = TicketList.STR_TIME_START_POUR;
        c_time_keys[2] = TicketList.C_TIME_START_POUR;
        times[3] = ticketView.time_finished_pour;
        times_frame[3] = ticketView.time_finished_pour_layout;
        str_time_keys[3] = TicketList.STR_TIME_FINISHED_POUR;
        c_time_keys[3] = TicketList.C_TIME_FINISHED_POUR;
        times[4] = ticketView.time_left_job;
        times_frame[4] = ticketView.time_left_job_layout;
        str_time_keys[4] = TicketList.STR_TIME_LEFT_JOB;
        c_time_keys[4] = TicketList.C_TIME_LEFT_JOB;
        times[5] = ticketView.time_arrived_plant;
        times_frame[5] = ticketView.time_arrived_plant_layout;
        str_time_keys[5] = TicketList.STR_TIME_ARRIVED_PLANT;
        c_time_keys[5] = TicketList.C_TIME_ARRIVED_PLANT;
        
        AppDelegate.sharedInstance().addNotificationListener(DataSourceChangeNotificationListener.class, this);
        
        Dictionary<String, Object> temp = new Hashtable<String, Object>();
        temp.put("type", "getTerm");
        AppDelegate.sharedInstance().sendHttpRequest("getTerm?", temp, this);
        for (int i = 0; i < times_frame.length; i++) {times_frame[i].removeView(btn_time);}
	}
	
	
	Timer timer = new Timer();  
    Handler handler = new Handler(){  
  
        public void handleMessage(Message msg) {  
        	time();
            super.handleMessage(msg);  
        }
    };  
    TimerTask task = new TimerTask(){  
  
        public void run() {  
            Message message = new Message();      
            message.what = 1;
            handler.sendMessage(message);    
        }
    };
	private void time() {
		ticketsDataSource.refresh();
	}
	
	public void dealloc() {
		AppDelegate.sharedInstance().layout.removeView(view);
		if (ticketsDataSource != null) ticketsDataSource.dealloc();
		ticketsDataSource = null;
		if (ticketView != null) ticketView.dealloc();
		ticketView = null;
		if (timer != null) timer.cancel();
		timer = null;
		if (btn_time != null) {
			btn_time.timer.cancel();
		}
		
		AppDelegate.sharedInstance().removeNotificationListener(DataSourceChangeNotificationListener.class, this);		
	}
	
	private void updateGoButton() {
		Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(ticketView.getTicketId()));
		if (c != null && c.get(TicketList.IS_GONING) != null) {
			btn_go.setTextColor(0xFF00FF00);
			int index = ((Integer)c.get(TicketList.CURRENT_TIME_INDEX)).intValue();
			if (index == 6) {
				for (int i = 0; i < times_frame.length; i++) {times_frame[i].removeView(btn_time);}
			} else {
				for (int i = 0; i < times_frame.length; i++) {times_frame[i].removeView(btn_time);}
				times_frame[index].addView(btn_time);
			}
			btn_go.setText(FilePack.getStringByID(STRING_ID.STR_GO));
		} else if (c != null && c.get(TicketList.BATCH_WEIGHT) != null) {
			btn_go.setTextColor(0xFF00FF00);
			btn_go.setText(FilePack.getStringByID(STRING_ID.STR_GO));
		} else {
			btn_go.setTextColor(0xFFFF0000);
			for (int i = 0; i < times_frame.length; i++) {times_frame[i].removeView(btn_time);}
			btn_go.setText(FilePack.getStringByID(STRING_ID.STR_GO));
		}
	}
	
	private void scheduled_ticket() {
		backButton.setVisibility(View.VISIBLE);
		list.setVisibility(View.VISIBLE);
		bottom.setVisibility(View.VISIBLE);
	    //CGAffineTransform trans = CGAffineTransformMakeScale(0.85, 0.85);
	    //trans = CGAffineTransformTranslate(trans, 110, -44);
	    //ticketView.transform = trans;
	    //ticketView.userInteractionEnabled = NO;
	}
	
	private void scheduled_ticket_back() {
		backButton.setVisibility(View.INVISIBLE);
		list.setVisibility(View.INVISIBLE);
		bottom.setVisibility(View.INVISIBLE);
	    //ticketView.transform = CGAffineTransformIdentity;
	    //ticketView.userInteractionEnabled = YES;
	}
	
	private void modify_ticket() {
		new AddWaterView(ticketView.getTicketId());
	}
	
	private void lock() {
		Vector<Dictionary<String, Object>> arr = ticketsDataSource.data;
		Dictionary<String, Object> dic = null;
		for (int i = 0; i < arr.size(); i++) {
			Dictionary<String, Object> ticket = arr.elementAt(i);
			if (IntConvert.parseLong(ticket.get("TicketID")) == ticketView.getTicketId()) {
				dic = ticket;
				break;
			}
		}
		if (dic != null) {
			Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(ticketView.getTicketId()));
			Date now = new Date();
			DateFormat d1 = DateFormat.getDateTimeInstance();
			String time = d1.format(now);
		    String CustomerName_1 = (String) dic.get("CustomerName_1");
		    String CustomerName_2 = (String) dic.get("CustomerName_2");
		    String CustomerName_3 = (String) dic.get("CustomerName_3");
		    if (CustomerName_1 == null) CustomerName_1 = "";
		    if (CustomerName_2 == null) CustomerName_2 = "";
		    if (CustomerName_3 == null) CustomerName_3 = "";
		    String CustomerName = CustomerName_1+"\n"+CustomerName_2+"\n"+CustomerName_3;
		    String order_code = (String) dic.get("OrderNum");
			String ticket_code = (String) dic.get("Ticket_");
			String batch_weights = (String) c.get(TicketList.BATCH_WEIGHT);
			if (IntConvert.parseInt(c.get(TicketList.HIDE_BATCH_WEIGHT)) == 1) {
				batch_weights = null;
			}

			
			
			Vector<Object> params = new Vector<Object>();
			String print = "date and time:"+time+
					"\ncustomer name:"+CustomerName+
					"\norder code:"+order_code+
					"\nticket code:"+ticket_code;
			if (batch_weights != null) {
				print += ("\nbatch weights:\n" + batch_weights);
			}
			params.addElement(print);
			params.addElement(Long.valueOf(ticketView.getTicketId()));
			new E_PrintView(params);
		}
	}
	
	private void go() {
		Dictionary<String, Object> memoery = ticketsDataSource.memoryObject.get(Long.valueOf(ticketView.getTicketId()));
		
		boolean canGo = false;
		for (int i = 0; i < ticketsDataSource.data.size(); i++) {
			Dictionary<String, Object> dic = ticketsDataSource.data.elementAt(i);
			long tid = IntConvert.parseLong(dic.get("TicketID"));
			if (tid == ticketView.getTicketId()) {
				Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(tid));
				if (c.get(TicketList.BATCH_WEIGHT) != null) {
					canGo = true;
				} else if (IntConvert.parseInt((String) dic.get(TicketList.DONT_NEED_BW)) == 1) {
					canGo = true;
				}
				break;
			}
		}
		
		if (canGo) {
			if (memoery.get(TicketList.IS_GONING) == null) {
				Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(ticketView.getTicketId()));
				
				c.put(TicketList.IS_GONING, "1");
				c.put(TicketList.CURRENT_TIME_INDEX, Integer.valueOf(0));
				
				updateGoButton();
			}
		}
	}
	
	private void sign_ticket() {
		new AcceptTerms(ticketView.getTicketId());
	}
	
	private void submit() {
		submitWithDeleteTicket(true);
	}
	
	public void submitWithDeleteTicket(boolean bDelete) {
		Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(ticketView.getTicketId()));
		if (c == null) c = new Hashtable<String, Object>();
		Dictionary<String, Object> upload = new Hashtable<String, Object>();
		IntConvert.dictionaryPut(upload, "time_left_plant", IntConvert.toString(c.get(TicketList.C_TIME_LEFT_PLANT)));
		IntConvert.dictionaryPut(upload, "time_arrived_job", IntConvert.toString(c.get(TicketList.C_TIME_ARRIVED_JOB)));
		IntConvert.dictionaryPut(upload, "time_start_pour", IntConvert.toString(c.get(TicketList.C_TIME_START_POUR)));
		IntConvert.dictionaryPut(upload, "time_finished_pour", IntConvert.toString(c.get(TicketList.C_TIME_FINISHED_POUR)));
		IntConvert.dictionaryPut(upload, "time_left_job", IntConvert.toString(c.get(TicketList.C_TIME_LEFT_JOB)));
		IntConvert.dictionaryPut(upload, "time_arrived_plant", IntConvert.toString(c.get(TicketList.C_TIME_ARRIVED_PLANT)));
		IntConvert.dictionaryPut(upload, "accept_terms", IntConvert.toString(c.get(TicketList.ACCEPT_TERMS)));
		IntConvert.dictionaryPut(upload, "signby", IntConvert.toString(c.get(TicketList.SIGN_NAME)));
		IntConvert.dictionaryPut(upload, "rejectby", IntConvert.toString(c.get(TicketList.REJECT_NAME)));
		IntConvert.dictionaryPut(upload, "rejectreason", IntConvert.toString(c.get(TicketList.REJECT_REASON)));
		IntConvert.dictionaryPut(upload, "driver_note", IntConvert.toString(c.get(TicketList.DRIVER_NOTE)));
		
		Vector<String> emails = new Vector<String>();
		if (c.get(TicketList.EMAILS) != null) {
			emails.addAll((Vector<String>) c.get(TicketList.EMAILS));
		}
		if (c.get(TicketList.CUSTOM_EMAILS) != null) {
			emails.addAll((Vector<String>) c.get(TicketList.CUSTOM_EMAILS));
		}
		String email = null;
		if (emails.size() > 0) {
			email = emails.elementAt(0);
			for (int i = 1; i < emails.size(); i++) {
				email += ";" + emails.elementAt(i);
			}
		}
		IntConvert.dictionaryPut(upload, "email", email);
		
		Vector<Bitmap> signature = (Vector<Bitmap>) c.get(TicketList.SIGNATURE);
		if (signature != null && signature.size() > 0) {
			for (int i = 0; i < signature.size(); i++) {
				upload.put("signature"+(i+1), signature.elementAt(i));
			}
		}
		upload.put("ticketID", ""+ticketView.getTicketId());
		if (IntConvert.parseInt(upload.get("accept_terms")) == 0  || (upload.get("signature1") == null && upload.get("rejectby") == null)) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle("please accept terms and signature or reject");
			alert.show();
		} else {
			waittingAlertView = WaittingAlertView.createWaittingAlertView();
			waittingAlertView.setLoadingText(FilePack.getStringByID(STRING_ID.STR_PLEASE_WAIT));
			waittingAlertView.show();
			
			Dictionary<String, Object> temp = new Hashtable<String, Object>();
			temp.put("ticketID", Long.valueOf(ticketView.getTicketId()));
			temp.put("type", "submit");
			temp.put("bDelete", "1");
			AppDelegate.sharedInstance().postHttpRequest("submitTicket", upload, temp, this, true);
		}
	}
	
	private void logout() {
		AppDelegate.sharedInstance().logout();
	}

	private void time_btn_clicked() {
		Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(ticketView.getTicketId()));
		int index = ((Integer)c.get(TicketList.CURRENT_TIME_INDEX)).intValue();
		times[index].setText(btn_time.getText());
		c.put(str_time_keys[index], times[index].getText());
		c.put(c_time_keys[index], Long.valueOf(btn_time.getCurrentTime()));
		if (index == 5) {
			for (int i = 0; i < times_frame.length; i++) {times_frame[i].removeView(btn_time);}
		} else {
			for (int i = 0; i < times_frame.length; i++) {times_frame[i].removeView(btn_time);}
			times_frame[index+1].addView(btn_time);
		}
		c.put(TicketList.CURRENT_TIME_INDEX, Integer.valueOf(index+1));
	}
	
	private void didSelectRowAtIndexPath(int index) {
		Dictionary<String, Object> dic = ticketsDataSource.data.elementAt(index);
		ticketView.setTicketId(IntConvert.parseLong(dic.get("TicketID")));
		updateGoButton();
		adapter.notifyDataSetInvalidated();
	}

	private void chooseTicket() {
		long tidckID = -1;
		
		for (int i = 0; i < ticketsDataSource.data.size(); i++) {
			Dictionary<String, Object> dic = ticketsDataSource.data.elementAt(i);
			long tid = IntConvert.parseLong(dic.get("TicketID"));
			Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(tid));
			if (c.get(TicketList.IS_GONING) != null) {
				tidckID = tid;
				break;
			}
		}
		
		if (tidckID == -1) {
			for (int i = 0; i < ticketsDataSource.data.size(); i++) {
				Dictionary<String, Object> dic = ticketsDataSource.data.elementAt(i);
				long tid = IntConvert.parseLong(dic.get("TicketID"));
				Dictionary<String, Object> c = ticketsDataSource.memoryObject.get(Long.valueOf(tid));
				if (c.get(TicketList.BATCH_WEIGHT) != null) {
		            tidckID = tid;
		            break;
				}
			}
		}
		
		if (tidckID == -1) {
			long ctime = 0x7fffffffffffffffL;
			for (int i = 0; i < ticketsDataSource.data.size(); i++) {
				Dictionary<String, Object> dic = ticketsDataSource.data.elementAt(i);
				long t = IntConvert.parseLong(dic.get("ctime"));
				if (t < ctime) {
					ctime = t;
					tidckID = IntConvert.parseLong(dic.get("TicketID"));
				}
			}
		}
		
		if (tidckID > 0) {
			ticketView.setTicketId(tidckID);
		}
	}

	
	public void notification(Class<?> clss, Object object, Object userInfo) {
		if (DataSourceChangeNotificationListener.class == clss) {
			if (object == ticketsDataSource) {
				updateGoButton();
				chooseTicket();
				adapter.notifyDataSetInvalidated();
			}
		}
		
	}


	public void requestFinished(RequestItem request, Dictionary<String, Object> params,
			Element _data, boolean successfull) {
		if (params.get("type").equals("submit")) {
			if (XMLReader.getIntForKey(_data, "ret", 0) == 1) {
				long ticketID = ticketView.getTicketId();
				ticketView.setTicketId(0);
				waittingAlertView.setTitle(FilePack.getStringByID(STRING_ID.STR_SUBMIT_SUCCESSFULL));
				//waittingAlertView.cancel();
				waittingAlertView = null;
				
				if (params.get("bDelete") != null) {
					Vector<Dictionary<String, Object>> arr = ticketsDataSource.data;
					for (int i = 0; i < arr.size(); i++) {
						Dictionary<String, Object> dic = arr.elementAt(i);
						if (IntConvert.parseLong(dic.get("TicketID")) == ticketID) {
							arr.removeElementAt(i);
							ticketsDataSource.saveCustomAndNotifyChange();
							break;
						}
					}
					ticketsDataSource.memoryObject.remove(Long.valueOf(ticketID));
					
					updateGoButton();
					chooseTicket();
				}
			} else {
				waittingAlertView.setTitle(XMLReader.getStringForKey(_data, "error", FilePack.getStringByID(STRING_ID.STR_ERROR_OCCUR)));
				waittingAlertView = null;
			}
		} else if (params.get("type").equals("getTerm")) {
			if (XMLReader.getIntForKey(_data, "ret", 0) == 1) {
				term = XMLReader.getStringForKey(_data, "Term", null);
			}
		}
	}
	
	private void showTicket() {
		ticketView.ticketLayout.setVisibility(View.VISIBLE);
		ticketView.mapLayout.setVisibility(View.INVISIBLE);
	}
	
	private void showMap() {
		ticketView.ticketLayout.setVisibility(View.INVISIBLE);
		ticketView.mapLayout.setVisibility(View.VISIBLE);
	}
	
	
	
	
	
	

	private final class ViewHolder {  
        private TextView ticket;
    }

	
	private final class MyAdapter extends BaseAdapter {  
        private LayoutInflater mInflater;
        public MyAdapter(Context context) {  
            this.mInflater = LayoutInflater.from(context);  
        }  
        public int getCount() {  
        	return ticketsDataSource.data.size();
        }  
        public Object getItem(int arg0) {  
            return ticketsDataSource.data.elementAt(arg0);  
        }  
        public long getItemId(int arg0) {  
            return arg0;  
        }  
        public View getView(int position, View convertView, ViewGroup parent) {  
            ViewHolder holder = null;  
            if (convertView == null) {  
                holder = new ViewHolder();  
                convertView = mInflater.inflate(R.layout.ticket_cell, null);  
                holder.ticket = (TextView) convertView.findViewById(R.id.title);  

                convertView.setTag(holder);
            } else {  
                holder = (ViewHolder) convertView.getTag();  
            }
            
            Dictionary<String, Object> dic = ticketsDataSource.data.elementAt(position);
            holder.ticket.setText((String)dic.get("TicketID"));
            if (IntConvert.parseLong(dic.get("TicketID")) == AppDelegate.sharedInstance().getMain().ticketView.getTicketId()) {
            	holder.ticket.setTextColor(0xFFFF0000);
            } else {
            	holder.ticket.setTextColor(0xFF000000);
            }
            return convertView;  
        }
    }
	
	
	
	
	
	
	private Button button_show_ticket;
	private Button button_show_map;
	private Button button_scheduled_ticket;
	private Button button_modify_ticket;
	private Button button_lock;
	private Button button_sign_ticket;
	private Button button_submit;
	private Button button_logout;
	private Button button_scheduled_ticket_back;
	private RelativeLayout ticket_view_layout;

	private void loadMainView() {
        view = (RelativeLayout)AppDelegate.sharedInstance().activity.getLayoutInflater().inflate(R.layout.main_view, null);
        list = (ListView) view.findViewById(R.id.main_list);
        button_show_ticket = (Button)view.findViewById(R.id.button_show_ticket);
        button_show_map = (Button)view.findViewById(R.id.button_show_map);
        button_scheduled_ticket = (Button)view.findViewById(R.id.button_scheduled_ticket);
        button_modify_ticket = (Button)view.findViewById(R.id.button_modify_ticket);
        button_lock = (Button)view.findViewById(R.id.button_lock);
        btn_go = (Button)view.findViewById(R.id.button_go);
        button_sign_ticket = (Button)view.findViewById(R.id.button_sign_ticket);
		button_submit = (Button)view.findViewById(R.id.button_submit);
		button_logout = (Button)view.findViewById(R.id.button_logout);
		backButton = (Button)view.findViewById(R.id.button_back_button);
		bottom = (RelativeLayout)view.findViewById(R.id.view_bottom);
		button_scheduled_ticket_back = (Button)view.findViewById(R.id.button_scheduled_ticket_back);
		ticket_view_layout = (RelativeLayout)view.findViewById(R.id.ticket_view_layout);

		//button_show_ticket.setText(FilePack.getStringByID(STRING_ID.STR_TICKET_INFORMATION));
		//button_show_map.setText(FilePack.getStringByID(STRING_ID.STR_DESTINATION_MAP));
		button_scheduled_ticket.setText(FilePack.getStringByID(STRING_ID.STR_SECHEDULED_TICKETS));
		button_modify_ticket.setText(FilePack.getStringByID(STRING_ID.STR_MODIFY_TICKET));
		button_lock.setText(FilePack.getStringByID(STRING_ID.STR_E_PRINT));
		btn_go.setText(FilePack.getStringByID(STRING_ID.STR_GO));
		button_sign_ticket.setText(FilePack.getStringByID(STRING_ID.STR_SIGN_TICKET));
		button_submit.setText(FilePack.getStringByID(STRING_ID.STR_SUBMIT));
		button_logout.setText(FilePack.getStringByID(STRING_ID.STR_LOGOUT));
		button_scheduled_ticket_back.setText(FilePack.getStringByID(STRING_ID.STR_SECHEDULED_TICKETS));
		
		if (FilePack.currentLanguage() == R.raw.c_en_us) {
			button_show_ticket.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.ticket_information_us));
			button_show_map.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.destination_map_us));
		} else {
			button_show_ticket.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.ticket_information_es));
			button_show_map.setBackgroundDrawable(view.getResources().getDrawable(R.drawable.destination_map_es));
		}
		
		button_show_ticket.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showTicket();
			}
		});
		button_show_map.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showMap();
			}
		});
		button_scheduled_ticket.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scheduled_ticket();
			}
		});
		button_modify_ticket.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				modify_ticket();
			}
		});
		button_lock.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				lock();
			}
		});
		button_show_ticket.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showTicket();
			}
		});
		btn_go.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				go();
			}
		});
		button_sign_ticket.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				sign_ticket();
			}
		});
		button_submit.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				submit();
			}
		});
		button_logout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				logout();
			}
		});
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scheduled_ticket_back();
			}
		});
		button_scheduled_ticket_back.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				scheduled_ticket_back();
			}
		});
		
		list.setVisibility(View.INVISIBLE);
		bottom.setVisibility(View.INVISIBLE);
		backButton.setVisibility(View.INVISIBLE);
		
		adapter = new MyAdapter(AppDelegate.sharedInstance().activity);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
	        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {  
	        	didSelectRowAtIndexPath(arg2);
	        }
		});

	}
}
